import { storeDetail, buy } from '../../../api/api.js';
import { operation } from '../../../utils/show.js';
const app = getApp();
var WxParse = require('../../../utils/wxParse/wxParse');
Page({
  data: {
    height: app.globalData.height * 2 + 20,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      // title: '兑积分  ', //导航栏 中间的标题
      img: "../../image/back.png"
    },
    state:false,
    i:1,
    swiperImgUrl: '',
    indicatorColor: "rgba(255,105,0,0.3)",
    indicatorActiveColor: "#FF6900",
    circular: true,
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1200,
    /* 新增 */
    detailsId: 0,
    title: '',
    goodsImg: '',
    goods_name: '',
    content: '',
    point: '0'
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      detailsId: options.id
    });
    app.showLoading();
    storeDetail({id: options.id}).then(res => {
      app.hideLoading();
      const data = res.data;
      const imgUrl = app.globalData.imgUrl;
      that.setData({
        title: data.goods_name,
        point: data.point,
        goodsImg: imgUrl + data.main_img,
        goods_name: data.goods_name,
        content: data.content
      });
      const htmler = data.content;
      WxParse.wxParse('htmler', 'html', htmler, that, 5);
    });
  },
  click:function(){
     var that=this;
     that.setData({
       state:!this.data.state
     }) 
  },
  plus:function(){
    let a = this.data.i;
    a++
    this.setData({
       i : a
    }) 
  },
  reduce:function(){
    let a = this.data.i;
    if(a!=0) a--;
    this.setData({
      i: a
    }) 
  },

  /**
  *用户点击兑换
  */
  dhButton() {
    const quantity = this.data.i;
    if (quantity < 1) {
      operation( "请选择商品数量", false); 
      return;
    }
    if (!app.globalData.hasLogin || !app.globalData.hasMobile) {
      operation( "需要注册用户并认证才可以兑换", false); 
      return;
    }
    const openid = app.globalData.openid;
    if (!openid) {
      app.showToast('openid为空');
      return;
    }
    const data = {
      goodsid: this.data.detailsId,
      quantity, openid
    }
    app.showLoading();
    buy(data).then(res => {
      app.hideLoading();
      switch (res.code) {
        case 0:
          let used_point = app.globalData.userInfo.used_point;
          used_point = parseInt(this.data.point) * parseInt(quantity) + parseInt(used_point);
          app.globalData.userInfo.used_point = used_point;
          operation( "您已成功兑换", false); 
          break;
        case 4:
          operation("需要认证才能兑换商品哦~", true, '去认证',
            function () {  wx.navigateTo({ url: '/pages/have/authentication/authentication' }); },
            function () { console.log('取消') }
          );
          break;
        case 5:
          operation( "积分余额不足", false); 
          break;
        default:
          app.showToast(res.msg);
          break;
      }
    })
    // if(true){
    //   operation(
    //     "需要认证才能兑换商品哦~",
    //     true,
    //     '去认证',
    //     function () {
    //       //用户点击确定之后

    //     },
    //     function () {
    //       //用户点击取消之后
    //     }
    //   ); 
    // }else if(false){

    // }else{
    //   operation(
    //     "积分余额不足",
    //     false
    //   ); 
    // }
 
  }
});
